﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.EntityFrameworkCore;
using StudentDB;

namespace StudentDBUI.Controllers
{
    public class ScsController : Controller
    {
        private readonly StudentDBContext _context;

        public ScsController(StudentDBContext context)
        {
            _context = context;
        }

        // GET: Scs
        public async Task<IActionResult> Index()
        {
            var studentDBContext = _context.Scs.Include(s => s.CnoNavigation).Include(s => s.SnoNavigation);
            return View(await studentDBContext.ToListAsync());
        }

        // GET: Scs/Details/5
        public async Task<IActionResult> Details(int? id)
        {
            if (id == null)
            {
                return NotFound();
            }

            var sc = await _context.Scs
                .Include(s => s.CnoNavigation)
                .Include(s => s.SnoNavigation)
                .FirstOrDefaultAsync(m => m.Id == id);
            if (sc == null)
            {
                return NotFound();
            }

            return View(sc);
        }

        // GET: Scs/Create
        public IActionResult Create()
        {
            ViewData["Cno"] = new SelectList(_context.Courses, "Cno", "Cno");
            ViewData["Sno"] = new SelectList(_context.Students, "Sno", "Sno");
            return View();
        }

        // POST: Scs/Create
        // To protect from overposting attacks, enable the specific properties you want to bind to.
        // For more details, see http://go.microsoft.com/fwlink/?LinkId=317598.
        [HttpPost]
        [ValidateAntiForgeryToken]
        public async Task<IActionResult> Create([Bind("Id,Sno,Cno,Grade")] Sc sc)
        {
            if (ModelState.IsValid)
            {
                _context.Add(sc);
                await _context.SaveChangesAsync();
                return RedirectToAction(nameof(Index));
            }
            ViewData["Cno"] = new SelectList(_context.Courses, "Cno", "Cno", sc.Cno);
            ViewData["Sno"] = new SelectList(_context.Students, "Sno", "Sno", sc.Sno);
            return View(sc);
        }

        // GET: Scs/Edit/5
        public async Task<IActionResult> Edit(int? id)
        {
            if (id == null)
            {
                return NotFound();
            }

            var sc = await _context.Scs.FindAsync(id);
            if (sc == null)
            {
                return NotFound();
            }
            ViewData["Cno"] = new SelectList(_context.Courses, "Cno", "Cno", sc.Cno);
            ViewData["Sno"] = new SelectList(_context.Students, "Sno", "Sno", sc.Sno);
            return View(sc);
        }

        // POST: Scs/Edit/5
        // To protect from overposting attacks, enable the specific properties you want to bind to.
        // For more details, see http://go.microsoft.com/fwlink/?LinkId=317598.
        [HttpPost]
        [ValidateAntiForgeryToken]
        public async Task<IActionResult> Edit(int id, [Bind("Id,Sno,Cno,Grade")] Sc sc)
        {
            if (id != sc.Id)
            {
                return NotFound();
            }

            if (ModelState.IsValid)
            {
                try
                {
                    _context.Update(sc);
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!ScExists(sc.Id))
                    {
                        return NotFound();
                    }
                    else
                    {
                        throw;
                    }
                }
                return RedirectToAction(nameof(Index));
            }
            ViewData["Cno"] = new SelectList(_context.Courses, "Cno", "Cno", sc.Cno);
            ViewData["Sno"] = new SelectList(_context.Students, "Sno", "Sno", sc.Sno);
            return View(sc);
        }

        // GET: Scs/Delete/5
        public async Task<IActionResult> Delete(int? id)
        {
            if (id == null)
            {
                return NotFound();
            }

            var sc = await _context.Scs
                .Include(s => s.CnoNavigation)
                .Include(s => s.SnoNavigation)
                .FirstOrDefaultAsync(m => m.Id == id);
            if (sc == null)
            {
                return NotFound();
            }

            return View(sc);
        }

        // POST: Scs/Delete/5
        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public async Task<IActionResult> DeleteConfirmed(int id)
        {
            var sc = await _context.Scs.FindAsync(id);
            _context.Scs.Remove(sc);
            await _context.SaveChangesAsync();
            return RedirectToAction(nameof(Index));
        }

        private bool ScExists(int id)
        {
            return _context.Scs.Any(e => e.Id == id);
        }
    }
}
